
const crypto = require('crypto');
let bootstrapConfig = require('./bootstrap').bootstrapConfig;

function makeSalt() {
    return crypto.randomBytes(16).toString('base64');
}

function getSalt() {
    return bootstrapConfig.crypto.salt;
    // return "nankatsu";
    // return makeSalt();
}

function encryptPassword(password, salt = getSalt()) {
    if (!password || !salt) return '';
    return crypto
        // .pbkdf2Sync(password, salt, 10000, 64, 'sha1')
        // .pbkdf2Sync(password, salt, 10000, 64, 'sha256')
        .pbkdf2Sync(password, salt, bootstrapConfig.crypto.iterations, bootstrapConfig.crypto.keylen, bootstrapConfig.crypto.digest)
        .toString('base64');
}

if (require.main === module) {
    const {
        argv
    } = require('yargs');
    process.stdout.write(encryptPassword(argv._[0].toString(), argv._[1]))
}

module.exports = {
    encryptPassword,
};